<template>
  <div>
    <el-table
      v-loading="loading"
      :data="list"
      style="width: 100%"
    >
      <el-table-column label="序号" width="60">
        <template slot-scope="scope">
          <span>{{(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}</span>
        </template>
      </el-table-column>
      <el-table-column label="手机号" prop="phone" />
      <el-table-column label="用户级别">
        <template slot-scope="scope">
          <span v-show="scope.row.userType == 0">普通用户</span>
          <span v-show="scope.row.userType == 1">电子卡用户</span>
          <span v-show="scope.row.userType == 2">车队用户</span>
        </template>
      </el-table-column>
      <el-table-column label="用户余额" prop="balance" />
      <el-table-column label="所属车队">
        <template slot-scope="scope">
          {{scope.row.fleetName ? scope.row.fleetName : '无'}}
        </template>
      </el-table-column>
      <el-table-column label="注册时间" prop="createdTime" />
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="text" @click="detail(scope.row)">订单详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="订单详情" :visible.sync="open" width="80%" append-to-body>
      <el-table
        v-loading="loading"
        :data="orderList"
        style="width: 100%"
        height="400"
      >
      <el-table-column label="序号" width="60">
        <template slot-scope="scope">
          <span>{{(order.pageNum - 1) * order.pageSize + scope.$index + 1}}</span>
        </template>
      </el-table-column>
      <el-table-column label="订单号" prop="orderNo" />
      <el-table-column label="设备名称"  prop="terminalName" />
      <el-table-column label="设备编号"  prop="terminalNo" />
      <el-table-column label="所属代理商" prop="agentName" />
      <el-table-column label="订单类型" >
        <template slot-scope="scope">
          <span v-show="scope.row.payMethod == 0">充值</span>
          <span v-show="scope.row.payMethod == 1">加注</span>
        </template>
      </el-table-column>
      <el-table-column label="体积" >
        <template slot-scope="scope">
          <span>{{scope.row.extraInfo + 'L'}}</span>
        </template>
      </el-table-column>
      <el-table-column label="订单金额">
        <template slot-scope="scope">
          {{scope.row.amount ? Number(scope.row.amount).toFixed(2) : '0'}}
        </template>
      </el-table-column>
      <el-table-column label="退款金额">
        <template slot-scope="scope">
          {{scope.row.refundAmount ? Number(scope.row.refundAmount).toFixed(2) : '0'}}
        </template>
      </el-table-column>
      <el-table-column label="返利金额" >
        <template slot-scope="scope">
          {{scope.row.rebateAmount ? Number(scope.row.rebateAmount).toFixed(2) : '0'}}
        </template>
      </el-table-column>
      <el-table-column label="最终金额">
        <template slot-scope="scope">
          {{scope.row.finalAmount ? Number(scope.row.finalAmount).toFixed(2) : '0'}}
        </template>
      </el-table-column>
      <el-table-column label="注册时间" prop="createdTime" />
      </el-table>
      <pagination v-show="orderTotal>0" :total="orderTotal" :page.sync="order.pageNum" :limit.sync="order.pageSize" @pagination="getOrderDetailList" />
    </el-dialog>
    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
  </div>
</template>

<script>
import { getUserList,getOrderDetail } from "@/api/common";
export default {
  data(){
    return{
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      orderList:[],
      order:{
        pageNum: 1,
        pageSize: 10,
      },
      orderTotal:0,
      detailItem:{},
      open:false
    }
  },
  created() {
    this.getList();
  },
  methods:{
    getList() {
      this.loading = true;
      getUserList(this.queryParams).then(response => {
        this.list = response.rows.map(item=>{
          return{
            ...item,
            balance:item.accountVo.balance,
          }
        })
        this.total = response.total;
        this.loading = false;
      });
    },
    detail(row){
      this.open = true
      this.getOrderDetailList(row.id)
    },
    getOrderDetailList(id){
      if(id && typeof id !== 'object') this.order.userId = id
      getOrderDetail(this.order).then(response=>{
        this.orderList = response.rows.map(item=>{
          const { amount,refundAmount,rebateAmount,extraInfo } = item
          let finalAmount = (amount && refundAmount && rebateAmount) ? (amount - refundAmount - rebateAmount) : 0
          return {
            ...item,
            extraInfo:extraInfo ? extraInfo : 0,
            refundAmount:refundAmount ? refundAmount : 0,
            rebateAmount:rebateAmount ? rebateAmount : 0,
            finalAmount
          }
        })
        this.orderTotal = response.total;
      })
    }
  }
}
</script>

<style>

</style>